<?php

	class System_Loader
	{

		public $currentModule = '';
		public $debug_info = array();
		public $output = '';

		private static $raw_data = null;

		public function __construct($module, $params = null)
		{
			if (strpos($module, '..') !== false) {
				throw new AppException('Security error!', System_Errors::ERROR_SYSTEM_SECURITY);
			}

			$action = DEFAULT_ACTION;
			if ($module != DEFAULT_MODULE) {
				if ($module[0] == '/') {
					$module = mb_substr($module, 1);
				}

				$params = is_null($params) ? $_REQUEST : $params;

				if (mb_substr($module, -1) != '/') {
					$action = basename($module);
					$module = dirname($module);
				} else {
					$module = mb_substr($module, 0, -1);
				}

				$module = str_replace('/', '_', $module);
			}

			if ($module == '.'){
				$module = DEFAULT_MODULE;
			}

			$moduleName = 'Module_' . $module;

			$this->output                       = $moduleName::$action($params);
			$this->debug_info['execution_time'] = round(microtime(true) - START_TIME, 4);
			$this->debug_info['queries']        = Database::$queries_count;

			if (is_array($this->output) || is_object($this->output)) {
				header('Content-type: text/javascript;charset=utf-8');
				$this->output = json_encode($this->output);
			}
		}

		public static function getRawData()
		{
			$data = file_get_contents('php://input');
			if (!empty($data)) {
				self::$raw_data = $data;
			}

			return self::$raw_data;
		}


	}